package com.itheima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.admin.mapper.AdUserMapper;
import com.itheima.admin.pojo.AdUser;
import com.itheima.admin.service.AdUserService;
import com.itheima.common.utils.JwtUtils;
import com.itheima.common.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 管理员用户信息表 服务实现类
 * </p>
 *
 * @author czh
 * @since 2021-08-19
 */
@Service
public class AdUserServiceImpl extends ServiceImpl<AdUserMapper, AdUser> implements AdUserService {

    @Autowired
    private AdUserMapper adUserMapper;
    /**
     * 用户用户密码
     * @param
     * @return
     */
    @Override
    public String login(AdUser adUser) {
        //得到用户名和密码
        String username = adUser.getName();
        String password = adUser.getPassword();
        //通过用户名查询是否存在
        QueryWrapper<AdUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",adUser.getName());
        AdUser user = adUserMapper.selectOne(queryWrapper);
        //判断用户是否存在
        if (user != null) {
            //得到盐
            String salt = user.getSalt();
            //与新密码加密与老密码比较
            if (Md5Util.toMD5(password,salt).equals(user.getPassword())){
                //相同登陆成功1,颁发令牌
                user.setPassword("*");
                Map<String,Object> beanMap = BeanMap.create(user);
                return JwtUtils.token(beanMap);

            }

        }


        return null;
    }
}
